home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jedit_modes.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  8.5 KB  |  289 lines

  1. # jedit_modes.tcl - mode-handling procedures for jedit, a tk-based editor
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non-profit, noncommercial use.
  5.  
  6. # TO DO
  7. #   ELIMINATE REDUNDANCY!  both in the code and in what's done when a mode
  8. #     is entered (I think prefs are being read more than once on startup).
  9. #   abbrev fixes:
  10. #     maybe some heuristics for things like plurals
  11. #     maybe a syntax for suffixes (e.g., commit;t -> commitment)
  12. #   file_modes panel
  13. #   documentation for keybindings (automatic documentation?)
  14. #   problem with filename getting set when you cancel Save 
  15. #     for the first time on a new unnamed file
  16. #   improve find panel
  17. #     have find wrap around (if last time didn't match)
  18. #     regex search/replace
  19. #     find all at once (mark) and cycle through with tag nextrange
  20. #   gesture commands
  21. #   autobreaking space a problem if you use two spaces betw sentences
  22. #   word-end punctuation (and heuristics) sd be mode-specific
  23. #
  24. #   PROBLEM WITH CHANGING BINDINGS ON THE FLY!               (urgent)
  25.  
  26. # CHANGES:
  27. #   lots of binding changes (jbind*.tcl consistency)
  28. #     app-specific Emacs and vi bindings
  29. #   house(s) the s won't expand
  30. #   return key checkpoints!
  31. #   improved mode handling (hooks)
  32.  
  33. ######################################################################
  34.  
  35. ######################################################################
  36. # jedit:guess_mode f - guess mode based on filename f
  37. ######################################################################
  38.  
  39. proc jedit:guess_mode { f } {
  40.   j:debug "jedit:guess_mode $f"
  41.   global FILE_MODES LINE_MODES
  42.   
  43.   #
  44.   # first, try matching on name
  45.   #
  46.   foreach i $FILE_MODES {
  47.     if [string match [lindex $i 0] $f] {
  48.       return [lindex $i 1]
  49.     }
  50.   }
  51.   #
  52.   # then, check first line (might be a script)
  53.   #
  54.   if {[file exists $f]} {
  55.     set file [open $f {r}]
  56.     set line1 [read $file 80]        ;# unix sees 32 chars, but be generous
  57.     close $file
  58.     foreach i $LINE_MODES {
  59.       if [string match [lindex $i 0] $line1] {
  60.         return [lindex $i 1]
  61.       }
  62.     }
  63.   }
  64.   #
  65.   # no matches - just use `plain'
  66.   #
  67.   return plain
  68. }
  69.  
  70. ######################################################################
  71. # jedit:read_mode_prefs m - read prefs in $JEDIT_MODEPREFS for mode m
  72. #   Note that the defaults _can't_ be in $jstools_library/jeditmodes .
  73. ######################################################################
  74.  
  75. proc jedit:read_mode_prefs { {m plain} } {
  76.   j:debug "Reading mode prefs for mode $m."
  77.   global JEDIT_MODEPREFS HOME
  78.   j:read_prefs -array JEDIT_MODEPREFS -prefix $m \
  79.     -directory $HOME/.tk/jeditmodes -file ${m}-defaults {
  80.     {textfont default}
  81.     {textwidth 80}
  82.     {textheight 24}
  83.     {textwrap char}
  84.     {sabbrev 0}
  85.     {dabbrev 0}
  86.     {autobreak 0}
  87.     {autoindent 0}
  88.     {parenflash 0}
  89.     {savestate 0}
  90.     {buttonbar 1}
  91.     {menu,editor 1}
  92.     {menu,file 1}
  93.     {menu,edit 1}
  94.     {menu,prefs 0}
  95.     {menu,abbrev 1}
  96.     {menu,filter 1}
  97.     {menu,format 0}
  98.     {menu,mode1 0}
  99.     {menu,mode2 0}
  100.     {menu,user 1}
  101.   }
  102. }
  103.  
  104. ######################################################################
  105. # jedit:set_default_mode_prefs mode -
  106. #   set defaults for all mode-specific preferences, so that failure
  107. #   of a mode to specify a particular preference doesn't trigger
  108. #   "no such variable" errors.  doesn't actually read a file.
  109. ######################################################################
  110.  
  111. proc jedit:set_default_mode_prefs { mode } {
  112.   global JEDIT_MODEPREFS
  113.   
  114.   set JEDIT_MODEPREFS($mode,textfont) default
  115.   set JEDIT_MODEPREFS($mode,textwidth) 80
  116.   set JEDIT_MODEPREFS($mode,textheight) 24
  117.   set JEDIT_MODEPREFS($mode,textwrap) char
  118.   set JEDIT_MODEPREFS($mode,sabbrev) 0
  119.   set JEDIT_MODEPREFS($mode,dabbrev) 0
  120.   set JEDIT_MODEPREFS($mode,autobreak) 0
  121.   set JEDIT_MODEPREFS($mode,autoindent) 0
  122.   set JEDIT_MODEPREFS($mode,autoindent) 0
  123.   set JEDIT_MODEPREFS($mode,parenflash) 0
  124.   set JEDIT_MODEPREFS($mode,buttonbar) 1
  125.   set JEDIT_MODEPREFS($mode,menu,editor) 1
  126.   set JEDIT_MODEPREFS($mode,menu,file) 1
  127.   set JEDIT_MODEPREFS($mode,menu,edit) 1
  128.   set JEDIT_MODEPREFS($mode,menu,prefs) 0
  129.   set JEDIT_MODEPREFS($mode,menu,abbrev) 1
  130.   set JEDIT_MODEPREFS($mode,menu,filter) 1
  131.   set JEDIT_MODEPREFS($mode,menu,format) 0
  132.   set JEDIT_MODEPREFS($mode,menu,mode1) 0
  133.   set JEDIT_MODEPREFS($mode,menu,mode2) 0
  134.   set JEDIT_MODEPREFS($mode,menu,user) 1
  135. }
  136.  
  137. ######################################################################
  138. # mode:plain:init - setup for plain mode
  139. #   the presence of this proc guarantees that jedit won't ever need
  140. #   to look for a "plain-mode.tcl" file.  (the same technique could
  141. #   be used by applications that embed jedit and need to modify its
  142. #   behaviour.)
  143. ######################################################################
  144.  
  145. proc mode:plain:init { t } {
  146.   global JEDIT_MODEPREFS
  147.   
  148.   j:read_prefs -array JEDIT_MODEPREFS -prefix plain \
  149.     -directory ~/.tk/jeditmodes -file plain-defaults {
  150.     {textfont default}
  151.     {textwidth 80}
  152.     {textheight 24}
  153.     {textwrap char}
  154.     {sabbrev 0}
  155.     {dabbrev 0}
  156.     {autobreak 0}
  157.     {autoindent 0}
  158.     {parenflash 0}
  159.     {savestate 0}
  160.     {buttonbar 1}
  161.     {menu,editor 1}
  162.     {menu,file 1}
  163.     {menu,edit 1}
  164.     {menu,prefs 0}
  165.     {menu,abbrev 1}
  166.     {menu,filter 1}
  167.     {menu,format 0}
  168.     {menu,mode1 0}
  169.     {menu,mode2 0}
  170.     {menu,user 1}
  171.   }
  172. }
  173.  
  174. ######################################################################
  175. # jedit:apply_mode -  apply current mode for window (menus, etc.)
  176. # the window can be specified either as a text widget, or as that
  177. # text widget's corresponding toplevel window.
  178. ######################################################################
  179.  
  180. proc jedit:apply_mode { w } {
  181.   j:debug "jedit:apply_mode $w"
  182.   global HOME JEDIT_MODEPREFS tk_library jstools_library
  183.   
  184.   if { [winfo class $w] == "Text" } {
  185.     set window [jedit:text_to_top $w]
  186.     set text $w
  187.   } else {
  188.     set window $w
  189.     set text [jedit:top_to_text $w]
  190.   }
  191.   set mode [jedit:get_mode $window]
  192.   set menubar [jedit:top_to_menubar $window]
  193.   set buttonbar [jedit:top_to_buttonbar $window]
  194.   
  195.   if [winfo exists $buttonbar] {
  196.     destroy $buttonbar
  197.   }
  198.   
  199.   foreach key {0 1 2 3 4 5 6 7 8 9} {
  200.     bind $text <Meta-Key-$key> { }
  201.   }
  202.   
  203.   jedit:set_default_mode_prefs $mode
  204.   
  205.   if {"x[info procs mode:${mode}:init]" == "x"} {
  206.     # we don't already have procs to enter the mode, so
  207.     # look through a list of directories searching for the mode file:
  208.     #
  209.     set file ${mode}-mode.tcl
  210.     foreach directory [list \
  211.       $HOME/.tk/jeditmodes \
  212.       $jstools_library/jeditmodes \
  213.       $jstools_library/jedit/jeditmodes \
  214.     ] {
  215.       if [file isfile $directory/$file] {
  216.         j:debug "Reading $file in $directory."
  217.         j:source_config -directory $directory $file
  218.         break
  219.       }
  220.     }
  221.   }
  222.   mode:${mode}:init $text
  223.   jedit:mkmenus $menubar $text
  224.   if $JEDIT_MODEPREFS($mode,buttonbar) {
  225.     jedit:mkbuttonbar $buttonbar $text
  226.     pack $buttonbar -after $menubar -fill x
  227.   }
  228.   if $JEDIT_MODEPREFS($mode,parenflash) {
  229.     jedit:balance_bind $text
  230.   }
  231.   jedit:configure_text $text
  232.   jedit:set_mode_label $window $mode
  233. }
  234.  
  235. ######################################################################
  236. # set the mode a particular window is in
  237. # the window can be specified either as a text widget, or as that
  238. # text widget's corresponding toplevel window.
  239. ######################################################################
  240.  
  241. proc jedit:set_mode { w newmode } {
  242.   j:debug
  243.   global MODES HOME
  244.   
  245.   if { [winfo class $w] == "Text" } {
  246.     set window [jedit:text_to_top $w]
  247.     set text $w
  248.   } else {
  249.     set window $w
  250.     set text [jedit:top_to_text $w]
  251.   }
  252.   
  253.   # do any cleanup necessary to cancel effect of current mode
  254.   set oldmode plain
  255.   catch {set oldmode $MODES($window)}
  256.   if {[info procs mode:$oldmode:cleanup] != {}} {
  257.     catch {mode:$oldmode:cleanup $text}
  258.   }
  259.   
  260.   set MODES($window) $newmode
  261. }
  262.  
  263. ######################################################################
  264. # return the mode a particular window is in
  265. # the window can be specified either as a text widget, or as that
  266. # text widget's corresponding toplevel window.  if no mode has been
  267. # set for that window, returns "plain".
  268. ######################################################################
  269.  
  270. proc jedit:get_mode { w } {
  271. #  j:debug
  272.   global MODES
  273.   
  274.   if { [winfo class $w] == "Text" } {
  275.     set window [jedit:text_to_top $w]
  276. #    set text $w
  277.   } else {
  278.     set window $w
  279. #    set text [jedit:top_to_text $w]
  280.   }
  281.   
  282.   if [info exists MODES($window)] {
  283.     return $MODES($window)
  284.   } else {
  285.     return {plain}
  286.   }
  287. }
  288.